
/**
 * The Application's header
 */
qx.Class.define("HersPOS.TableBar",
{
  extend : qx.ui.toolbar.ToolBar,

  events :
  {
    "done"   : "qx.event.type.Event",
    "reload" : "qx.event.type.Event"
  },

  construct : function()
  {
    this.base(arguments);

    //     var layout= qx.ui.layout.VBox()
    //     this.setLayout(layout)
    //     this.InsertBar = new qx.ui.toolbar.ToolBar();
    //     this.InsertBar.setDecorator(null)
    var common_text_setting =
    {
      alignY     : "middle",
      alignX     : "center",
      allowGrowY : false,
      readOnly   : false
    };

    var edit_tb_part = new qx.ui.toolbar.Part();
    var items_lbl = new qx.ui.basic.Label("Product Name :").set({ alignY : "middle" });

    var item_selbox = new qx.ui.form.SelectBox("Select Food :").set(
    {
      alignY     : "middle",
      allowGrowY : false
    });

    var count_lbl = new qx.ui.basic.Label("Total Items :").set({ alignY : "middle" });

    var total_cnt = new qx.ui.form.Spinner().set(
    {
      alignY     : "middle",
      allowGrowY : false
    });

    this.add_btn = new qx.ui.toolbar.Button("Add", "HersPOS/qx/icon/Oxygen/16/actions/list-add.png").set({ alignY : "middle" });

    var total_info_part = new qx.ui.toolbar.Part();
    var total_lbl = new qx.ui.basic.Label("Totals").set({ alignY : "middle" });
    var total_sold_lbl = new qx.ui.basic.Label("Items :").set({ alignY : "middle" });
    var total_sold_txt = new qx.ui.form.TextField().set(common_text_setting);
    var total_price_lbl = new qx.ui.basic.Label("Income :").set({ alignY : "middle" });
    var total_price_txt = new qx.ui.form.TextField().set(common_text_setting);
    var total_profit_lbl = new qx.ui.basic.Label("Profit :").set({ alignY : "middle" });
    var total_profit_txt = new qx.ui.form.TextField().set(common_text_setting);

    edit_tb_part.add(items_lbl);
    edit_tb_part.add(item_selbox);

    edit_tb_part.add(count_lbl);
    edit_tb_part.add(total_cnt);

    //  edit_tb_part.addSeparator()
    edit_tb_part.add(this.add_btn);

    total_info_part.add(total_lbl);
    total_info_part.addSeparator();
    total_info_part.add(total_sold_lbl);
    total_info_part.add(total_sold_txt);
    total_info_part.add(total_price_lbl);
    total_info_part.add(total_price_txt);
    total_info_part.add(total_profit_lbl);
    total_info_part.add(total_profit_txt);
    var url = "/HersPOS/default/get_items.json";
    var totals_url = "/HersPOS/default/get_totals.json";
    this.productlist_store = new qx.data.store.Json(url);
    this.totals_model = new qx.data.store.Json(totals_url);
    this.productlist_controller = new qx.data.controller.List(null, item_selbox);

    this.productlist_controller.setDelegate(
    {
      bindItem : function(controller, item, index)
      {
        controller.bindProperty("name", "label", null, item, index);
        controller.bindProperty("id", "model", null, item, index);
      }
    });

    this.productlist_store.bind("model", this.productlist_controller, "model");
    this.totals_model.bind("model.total_price", total_price_txt, "value");
    this.totals_model.bind("model.total_profit", total_profit_txt, "value");
    this.totals_model.bind("model.total_items", total_sold_txt, "value");

    this.add_btn.addListener("execute", function(e)
    {
      selection = item_selbox.getSelection();
      food_id = selection[0].getModel();
      total_sold = total_cnt.getValue();
      this.post_sales(food_id, total_sold);
    },
    this);

    this.post_sales = function(id, amt)
    {
      var req = new qx.io.request.Xhr("/HersPOS/default/insert_sales.json?food_id=" + id + "&amnt=" + amt);

      req.addListener("success", function(e)
      {
        var req = e.getTarget();
        this.fireEvent("done");

        // Response parsed according to the server's
        // response content type, e.g. JSON
        req.getResponse();
      },
      this);

      // Send request
      req.send();
    };

    this.addListener("done", function()
    {
      this.totals_model.reload();
      this.fireEvent("reload");
    },
    this);

    this.add(edit_tb_part);

    //     this.addSpacer()
    this.add(total_info_part);
  }
});

//     this.add(this.InsertBar,{flex:1})